package com.sc.nft.dao;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.TradingPoolOrder;
import com.sc.nft.entity.vo.DingLianDealPracticalVO;
import com.sc.nft.entity.vo.TradingPoolOrderSynchronizationVO;
import com.sc.nft.enums.OrderStatusEnum;
import com.sc.nft.enums.TradingPoolTransactionTypeEnum;
import com.sc.nft.mapper.TradingPoolOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author PC
 * @description 针对表【sc_trading_pool_order(生态池订单表)】的数据库操作Dao
 * @createDate 2024-10-26 13:55:50
 */
@Slf4j
@Repository
public class TradingPoolOrderDao extends ServiceImpl<TradingPoolOrderMapper, TradingPoolOrder> {


    /**
     * 更新订单结算状态
     *
     * @param version 版本号
     * @param flag    状态
     * @param id      主键id
     * @return int
     */
    public int allReadySettleFlag(Integer version, Boolean flag, Long id) {
        TradingPoolOrder tradingPoolOrder = new TradingPoolOrder();
        tradingPoolOrder.setId(id);
        tradingPoolOrder.setVersion(version);
        tradingPoolOrder.setShareSettleFlag(flag);
        return baseMapper.updateById(tradingPoolOrder);
    }

    /**
     * 根据类型和时间分页查询交易池订单
     *
     * @param page            分页
     * @param startDate       开始时间
     * @param endDate         结束时间
     * @param userId          用户id
     * @param transactionType 买入/卖出类型
     * @return TradingPoolOrder
     */
    public Page<TradingPoolOrder> pageTradeInOrder(Page<TradingPoolOrder> page, Date startDate, Date endDate, Long userId, Integer transactionType) {
        LambdaQueryWrapper<TradingPoolOrder> wrapper = Wrappers.lambdaQuery();
        wrapper.between(Objects.nonNull(startDate) && Objects.nonNull(endDate), TradingPoolOrder::getDealTime, startDate, endDate);
        wrapper.eq(TradingPoolOrder::getUserId, userId);
        wrapper.eq(TradingPoolOrder::getTransactionType, transactionType);
        wrapper.orderByDesc(TradingPoolOrder::getId);
        return baseMapper.selectPage(page, wrapper);
    }

    /**
     * 根据交易池id查询订单
     *
     * @param confId 配置id
     * @return
     */
    public List<TradingPoolOrder> listByConfId(Long confId) {
        LambdaQueryWrapper<TradingPoolOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TradingPoolOrder::getTradingPoolConfigId, confId);
        return list(wrapper);
    }

    /**
     * 获取交易记录
     *
     * @param id
     * @param page
     * @return
     */
    public Page<TradingPoolOrder> pageTransactionRecords(Long id, Page<TradingPoolOrder> page, Long daoId) {
        LambdaQueryWrapper<TradingPoolOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TradingPoolOrder::getTradingPoolConfigId, id);
        wrapper.eq(TradingPoolOrder::getDaoId, daoId);
        wrapper.orderByDesc(TradingPoolOrder::getDealTime);
        return page(page, wrapper);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    public IPage<TradingPoolOrder> orderPage(IPage<TradingPoolOrder> page, Long daoId, String orderNo, TradingPoolTransactionTypeEnum transactionType, Long userId, Date startDate, Date endDate) {
        LambdaQueryWrapper<TradingPoolOrder> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(daoId), TradingPoolOrder::getDaoId, daoId);
        wrapper.eq(StrUtil.isNotBlank(orderNo), TradingPoolOrder::getOrderNo, orderNo);
        wrapper.eq(Objects.nonNull(transactionType), TradingPoolOrder::getTransactionType, transactionType);
        wrapper.eq(Objects.nonNull(userId), TradingPoolOrder::getUserId, userId);
        wrapper.ge(Objects.nonNull(startDate), TradingPoolOrder::getDealTime, startDate);
        wrapper.le(Objects.nonNull(endDate), TradingPoolOrder::getDealTime, endDate);
        wrapper.orderByDesc(TradingPoolOrder::getPayTime);
        wrapper.orderByDesc(TradingPoolOrder::getId);
        return page(page, wrapper);
    }


    /**
     * 通过订单编号查询订单和排除类型
     *
     * @param orderNo 订单编号
     * @return TradingPoolOrder
     */
    public List<TradingPoolOrder> getOrderNoAndNotTradeType(String orderNo) {
        LambdaQueryWrapper<TradingPoolOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TradingPoolOrder::getOrderNo, orderNo);
        return list(wrapper);
    }

    /**
     * 获取结算订单
     *
     * @return
     */
    public List<TradingPoolOrder> getWaitSettleOrder() {
        return lambdaQuery().eq(TradingPoolOrder::getStatus, OrderStatusEnum.PAYMENT).eq(TradingPoolOrder::getSettlementFlag, false).orderByAsc(TradingPoolOrder::getId).list();
    }

    /**
     * 同步今日成交数据库
     *
     * @param tradingPoolId
     * @return
     */
    public TradingPoolOrderSynchronizationVO getOrderSynchronizationByTradingPoolId(Long tradingPoolId) {
        return baseMapper.getOrderSynchronizationByTradingPoolId(tradingPoolId);
    }

    public List<DingLianDealPracticalVO> dealTradingPracticalInConsensus() {
        return baseMapper.dealTradingPracticalInConsensus();
    }

    public List<DingLianDealPracticalVO> dealTradingPracticalNotInConsensus() {
        return baseMapper.dealTradingPracticalNotInConsensus();
    }

}
